package com.ltmall.api.rule;

import com.ltmall.api.manager.LogisticManager;

import java.util.*;

public class OrderRule {

    public void test() {
        List<CSG> list = generateCSGList();

        Set<CSG> hs = new HashSet<CSG>();
        hs.addAll(list);
        System.out.println(hs.size());

        // 所有可以拼包的订单
        List<List<CSG>> samePin = samePin(hs);
        System.out.println("圈子大小---》" + samePin.size());

        // 这是所有所有其余相同的shopID地址的商家
        List<List<CSG>> sameShop = sameShop(list, hs);
        System.out.println("剩余相同商店大小---》" + sameShop.size());

        // 将其余的相同的shopID的商家的订单加到一起融合生成的订单
        List<List<CSG>> orderList = combineOrder(samePin, sameShop);
        System.out.println("订单大小---》" + orderList.size());

    }

    // 这是整合的订单列表
    public static List<List<CSG>> combineOrder(List<List<CSG>> samePin, List<List<CSG>> sameShop) {
        for (List<CSG> pinList : samePin) {
            List<Integer> shopIDList = new ArrayList<Integer>();
            for (CSG csg : pinList) {
                shopIDList.add(csg.getShopID());
            }

            for (List<CSG> cList : sameShop) {
                // 因为都是相同的取第一个就可以了
                Integer sameShopID = cList.get(0).getShopID();
                if (shopIDList.contains(sameShopID)) {
                    for (CSG csg : cList) {
                        pinList.add(csg);
                    }
                    continue;
                }
            }
        }
        return samePin;
    }

    // 根据物流模板配置订单
    public static List<List<CSG>> logisticOrder(List<List<CSG>> orderList, LogisticManager logiManager) {

        List<List<CSG>> returnList = new ArrayList<List<CSG>>();
        Map<Integer, List<CSG>> map = new HashMap<Integer, List<CSG>>();
        for (List<CSG> oList : orderList) {

            //只有一个商品的跳过
            if (oList.size() == 1) {
                returnList.add(oList);
                continue;
            }

            //根据物流模板编号进行分组，相同物流模板的在一组
            for (CSG csg : oList) {
                int logiType = logiManager.logiType(csg.getGoodsID());
                if (map.containsKey(logiType)) {
                    map.get(logiType).add(csg);
                } else {
                    List<CSG> csgs = new ArrayList<CSG>();
                    csgs.add(csg);
                    map.put(logiType, csgs);
                }
            }
        }

        for (Map.Entry<Integer, List<CSG>> entry : map.entrySet()) {
            returnList.add(entry.getValue());
        }

        return returnList;
    }

    /*
     * pinID=0表示没有圈子
     */
    public static List<List<CSG>> samePin(Set<CSG> set) {
        List<List<CSG>> lList = new ArrayList<List<CSG>>();
        // 先获取不同的拼ID
        List<Integer> iList = new ArrayList<Integer>();
        for (CSG csg : set) {
            int pinID = csg.getPinID();
            System.out.println("拼ID" + pinID);
            if (pinID != 0) {
                iList.add(pinID);// 这里面都是有圈子的
            }
        }

        // 去重
        Set<Integer> iSet = new HashSet<Integer>();
        iSet.addAll(iList);

        // 这里面加的是有圈子的csg
        for (int pinID : iSet) {
            List<CSG> cList = new ArrayList<CSG>();
            for (CSG csg : set) {
                if (csg.getPinID() == pinID) {
                    cList.add(csg);
                }
            }
            lList.add(cList);
        }

        // 这里面加的是没有圈子的csg
        for (CSG csg : set) {
            List<CSG> cList = new ArrayList<CSG>();
            if (csg.getPinID() == 0) {
                cList.add(csg);
                lList.add(cList);
                continue;
            }
        }

        return lList;
    }

    public static List<List<CSG>> sameShop(List<CSG> list, Set<CSG> set) {
        List<List<CSG>> lList = new ArrayList<List<CSG>>();
        for (CSG CSG : set) {
            List<CSG> ll = new ArrayList<CSG>();
            for (CSG allCSG : list) {
                // 商店ID相同，且购物车ID不同
                if ((allCSG.getShopID() == CSG.getShopID()) && (allCSG.getCartID() != CSG.getCartID())) {
                    ll.add(allCSG);
                }
            }
            if (ll.size() != 0) {
                lList.add(ll);
            }
        }
        return lList;
    }

    // 测试数据
    public List<CSG> generateCSGList() {
        List<CSG> list = new ArrayList<CSG>();

        CSG csg = new CSG();
        csg.setCartID(1);
        csg.setShopID(5);
        csg.setGoodsID(0);
        csg.setPinID(0);

        CSG csg1 = new CSG();
        csg1.setCartID(2);
        csg1.setShopID(5);
        csg1.setGoodsID(0);
        csg1.setPinID(0);

        CSG csg2 = new CSG();
        csg2.setCartID(3);
        csg2.setShopID(5);
        csg2.setGoodsID(0);
        csg2.setPinID(0);

        CSG csg3 = new CSG();
        csg3.setCartID(4);
        csg3.setShopID(6);
        csg3.setGoodsID(0);
        csg3.setPinID(100);

        CSG csg4 = new CSG();
        csg4.setCartID(5);
        csg4.setShopID(6);
        csg4.setGoodsID(0);
        csg4.setPinID(100);

        CSG csg5 = new CSG();
        csg5.setCartID(6);
        csg5.setShopID(7);
        csg5.setGoodsID(0);
        csg5.setPinID(100);

        list.add(csg);
        list.add(csg1);
        list.add(csg2);
        list.add(csg3);
        list.add(csg4);
        list.add(csg5);
        return list;

    }
}
